HDU 4611 Balls Rearrangement(2013多校2 1001题)
Balls Rearrangement
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 25 Accepted Submission(s): 8
Problem Description
Bob has N balls and A boxes. He numbers the balls from 0 to N-1, and numbers the boxes from 0 to A-1. To find the balls easily, he puts the ball numbered x into the box numbered a if x = a mod A. Some day Bob buys B new boxes, and he wants to rearrange the balls from the old boxes to the new boxes. The new boxes are numbered from 0 to B-1. After the rearrangement, the ball numbered x should be in the box number b if x = b mod B.
This work may be very boring, so he wants to know the cost before the rearrangement. If he moves a ball from the old box numbered a to the new box numbered b, the cost he considered would be |a-b|. The total cost is the sum of the cost to move every ball, and it is what Bob is interested in now.
This work may be very boring, so he wants to know the cost before the rearrangement. If he moves a ball from the old box numbered a to the new box numbered b, the cost he considered would be |a-b|. The total cost is the sum of the cost to move every ball, and it is what Bob is interested in now.
Input
The first line of the input is an integer T, the number of test cases.(0<T<=50)
Then T test case followed. The only line of each test case are three integers N, A and B.(1<=N<=1000000000, 1<=A,B<=100000).
Then T test case followed. The only line of each test case are three integers N, A and B.(1<=N<=1000000000, 1<=A,B<=100000).
Output
For each test case, output the total cost.
Sample Input
3
1000000000 1 1
8 2 4
11 5 3
Sample Output
0
8
16
Source
Recommend
zhuyuanchen520
相当于求 abs(i%A - i%B)对i从0~N-1求和
题目给了N,A,B;
数据比较大。
首先可以确定的是A,B的LCM是一个循环。
然后一段的话,用模拟,相同段直接跳过求解,
#include <stdio.h> #include <algorithm> #include <iostream> #include <string.h> #include <set> #include <map> #include <vector> #include <queue> #include <string> #include <math.h> using namespace std; long long gcd(long long a,long long b) { if(b==0)return a; else return gcd(b,a%b); } long long lcm(long long a,long long b) { return a/gcd(a,b)*b; } long long calc(int n,int a,int b) { long long ans = 0; int i = 0; int ta=0,tb=0; int p = 0; while(i < n) { if(ta+a >= n && tb+b >= n) { ans += (long long)(n-i)*p; i = n; continue; } if(ta+a < tb+b) { ans += (long long)p*(ta+a-i); i = ta+a; p = i - tb; ta+=a; } else if(ta+a==tb+b) { ans+= (long long)p*(ta+a-i); i = ta+a; ta+=a; tb+=b; p = 0; } else { ans += (long long)p*(tb+b-i); i = tb+b; tb+= b; p = i-ta; } } return ans; } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int T; int n,a,b; scanf("%d",&T); while(T--) { scanf("%d%d%d",&n,&a,&b); if(a==b) { printf("0\n"); continue; } if(a < b)swap(a,b); long long LCM = lcm(a,b); if(LCM >= n) { printf("%I64d\n",calc(n,a,b)); continue; } long long tmp = calc(LCM,a,b); long long ans = tmp * (n/LCM)+calc(n%LCM,a,b); printf("%I64d\n",ans); } return 0; }
人一我百!人十我万!永不放弃~~~怀着自信的心,去追逐梦想